<?php
class Zendvn_View_Helper_ZenjooFormElements extends Zend_View_Helper_Abstract {
	
	public function zenjooFormElements($fileConfigs = null,$items = array(),$preData = array(),$patternFile = null) {
		$view= $this->view;
		$configObj = new Zendvn_Config($fileConfigs);
		$arrElements 	= $configObj->getConfig("element");
		unset($configObj);
		
		$listElements = explode("|",$arrElements['list']);
		array_shift($arrElements);
		foreach ($arrElements as $key => $val){
			$title['val'] = $view->translate($val['label']);
			$title['labelAtribs'] = 'for="'.$val['name'].'"';
			if ($val['require']){
				$title['val'] .='<span class="star">&nbsp;*</span>';
				$title['labelAtribs'] .=' class="required" '; 
			}
			
			$input['tooltip'] = $val['tooltip'];
			$atribs = explode("|", $val['attribs']);
			foreach ($atribs as $arr){
				$tmp = explode("=", $arr);
				$atribsArr[$tmp[0]] = $tmp[1];
			}
			
			$option = $val['options'];
			switch ($option['type']){
				case "loadFromControl":
						$optionArr = $preData[$option['source']];
						break;
				default:
						$opt = explode("|", $option['data']);
						if(count($opt >0))foreach ($opt as $arr){
							$tmp = explode("=", $arr);
							$optionArr[$tmp[1]] = $tmp[0];
						}	
						break;
			}
			
			$input   = $view->$val['contentType']($val['name'],$items[$val['name']],$atribsArr,$optionArr);
			if (isset($val['ajaxArea'])){
				$ajaxAreaId = str_replace("+",'"', $val['ajaxArea']);
				$input = '<div '.$ajaxAreaId.'>'.$input.'</div>';
			} 
			$xhtml .= $this->adminPattern($title, $input);
			unset($atribsArr);
			unset($optionArr);
		}
		return $xhtml;
	}
	public function adminPattern($title,$input){
		return  '<li><label '.$title['labelAtribs'].'>'.$title['val'].'
				</label>'.$input.' </li>';
	}
}